# coding: utf-8
from hashlib import md5

from django.db import models
from django.contrib.auth.models import User

# managers
class GameProfileManager(models.Manager):
    def create(self, user):
        obj = self.model()
        obj.save()
        return obj

class MarkerProfileManager(models.Manager):
    def create(self, user):
        obj = self.model()
        obj.save()
        return obj

class UserProfileManager(models.Manager):
    def create(self, user):
        obj = self.model(user=user)
        obj.save()
        return obj

# profiles
class GameProfile(models.Model):
    objects = GameProfileManager()


class MarketProfile(models.Model):
    objects = MarkerProfileManager()

    def get_balance(self):
        pass


class UserProfile(models.Model):
    objects = UserProfileManager()
    user = models.OneToOneField(User)
    gameprofile = models.OneToOneField(GameProfile, null=True)
    marketprofile = models.OneToOneField(MarketProfile, null=True)

    last_activation = models.DateTimeField(default='1970-01-01 01:01')
    activation_key = models.CharField(max_length=12)
    reputation = models.IntegerField(default=0)

    AD_SELECT = {
        'about': 'Обо мне',
        'skype': 'Skype',
        'site': 'Сайт',
        'icq': 'ICQ',
    }

    def get_avatar(self):
        return 'http://gravatar.com/avatar/' + md5(self.user.email)

    def is_on_site(self):
        return False

    def gp(self):
        if not self.gameprofile:
            self.gameprofile = GameProfile.objects.create(self.user)
        return self.gameprofile

    def mp(self):
        if not self.marketprofile:
            self.marketprofile = MarketProfile.objects.create(self.user)
        return self.marketprofile

